import 'package:cbec_app/components/common/common_style.dart';
import 'package:cbec_app/routes/routes.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../../pages/main/controller/main_ctr.dart';
import '../../utils/EventMgr.dart';
import 'coupon_shape_border.dart';

/**
 * 单个优惠券
 */
class MyCouponItem extends StatefulWidget {
  final int status; //状态

  final Map item;

  final Widget? rightWidgt;

  const MyCouponItem({
    super.key,
    required this.status,
    required this.item,
    this.rightWidgt,
  });

  @override
  State<MyCouponItem> createState() => _CouponItemState();
}

class _CouponItemState extends State<MyCouponItem> {
  final bool _is_open = false;

  late Color buttonColor;
  late String txt;

  @override
  Widget build(BuildContext context) {
    switch (widget.status) {
      case 0:
        //待开始
        buttonColor = Tools.fromHex("#FE5568");
        txt = "去使用";
      case 1:
        //已使用
        buttonColor = Tools.fromHex("#CCCCCC");
        txt = "已使用";
      case 2:
        //已过期
        txt = "已过期";
        buttonColor = Tools.fromHex("#CCCCCC");
      case 3:
        //去领取
        buttonColor = Tools.fromHex("#FE5568");
        txt = "去领取";
      default:
        buttonColor = Tools.fromHex("#FE5568");
        txt = "去领取";
    }
    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        ClipPath(
          clipper: ShapeBorderClipper(
            shape: CouponShapeBorder(),
          ),
          child: Container(
              height: 90.h,
              constraints: BoxConstraints(maxHeight: 200.w),
              margin: EdgeInsets.only(bottom: 10.w),
              color: const Color.fromRGBO(255, 255, 255, 1),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Row(
                    children: [
                      //左
                      Container(
                        width: 94.w,
                        padding: EdgeInsets.all(5.w),
                        color: buttonColor,
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            //${widget.item['drop']}
                            Text(
                              "${widget.item['drop']}",
                              style: const TextStyle(
                                fontSize: 19,
                                fontWeight: FontWeight.w700,
                                color: Colors.white,
                              ),
                            ),
                            SizedBox(height: 3.w),
                            Text(
                              "${widget.item['content'] ?? ''}",
                              textAlign: TextAlign.center,
                              style: const TextStyle(fontSize: 12, color: Colors.white),
                              overflow: TextOverflow.ellipsis,
                              maxLines: 2,
                            )
                          ],
                        ),
                      ),
                      //中
                      Container(
                        width: ScreenUtil().screenWidth * 0.4,
                        margin: EdgeInsets.fromLTRB(10.w, 0.w, 0.w, 0.w),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.start,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            const SizedBox(height: 10),
                            Text(widget.item['name'] ?? '',
                                style: const TextStyle(
                                  fontSize: 14,
                                  fontWeight: FontWeight.w500,
                                  color: Color.fromRGBO(51, 51, 51, 1),
                                ),
                                overflow: TextOverflow.ellipsis,
                                maxLines: 2),
                            const SizedBox(height: 5),
                            Text(
                              widget.item['goodsType'] == 0 ? '全部商品可用' : '部分商品可用',
                              style: const TextStyle(
                                fontSize: 10,
                                fontWeight: FontWeight.w300,
                                color: Color.fromRGBO(102, 102, 102, 1),
                              ),
                            ),
                            SizedBox(height: 2.w),
                            widget.item['validityType'] != 1
                                ? Row(
                                    children: [
                                      SizedBox(
                                        // width: 55.w,
                                        child: Text(
                                          "${widget.item['validityStartTime']}",
                                          style: const TextStyle(
                                            fontSize: 10,
                                            fontWeight: FontWeight.w300,
                                            color: Color.fromRGBO(102, 102, 102, 1),
                                          ),
                                        ),
                                      ),
                                      Container(
                                        margin: EdgeInsets.only(left: 5.w, right: 5.w),
                                        child: const Text(
                                          "—",
                                          style: TextStyle(
                                            fontSize: 10,
                                            fontWeight: FontWeight.w300,
                                            color: Color.fromRGBO(102, 102, 102, 1),
                                          ),
                                        ),
                                      ),
                                      SizedBox(
                                        // width: 55.w,
                                        child: Text(
                                          "${widget.item['validityEndTime']}",
                                          style: const TextStyle(
                                            fontSize: 10,
                                            fontWeight: FontWeight.w300,
                                            color: Color.fromRGBO(102, 102, 102, 1),
                                          ),
                                        ),
                                      ),
                                    ],
                                  )
                                : const Text(
                                    "长期有效",
                                    style: TextStyle(
                                      fontSize: 10,
                                      fontWeight: FontWeight.w300,
                                      color: Color.fromRGBO(
                                        102,
                                        102,
                                        102,
                                        1,
                                      ),
                                    ),
                                  ),
                          ],
                        ),
                      ),
                    ],
                  ),
                  //右【按钮】
                  widget.rightWidgt == null
                      ? Container(
                          margin: EdgeInsets.only(right: 10.w),
                          height: 24,
                          child: Center(
                            child: ElevatedButton(
                              style: ButtonStyle(
                                  //
                                  backgroundColor: WidgetStateProperty.all(buttonColor)),
                              onPressed: () {
                                if (widget.status == 0) {
                                  // Get.until((route) => Get.currentRoute ==Routes.main);
                                  // EventMgr.send(EventType.exchangeNav,MainController.mainBottomNavRouteNameHome);
                                  // Get.back();
                                  Get.until((route) => Get.currentRoute == Routes.main);
                                  EventMgr.send(EventType.exchangeNav, MainController.mainBottomNavRouteNameHome);
                                }
                              },
                              child: Text(
                                txt,
                                style: const TextStyle(
                                  fontSize: 10,
                                  fontWeight: FontWeight.w300,
                                  color: Colors.white,
                                ),
                              ),
                            ),
                          ))
                      : widget.rightWidgt!
                ],
              )),
        ),
        Visibility(
            visible: _is_open, //couponCtr.isCloses.value,
            child: Container(
              padding: EdgeInsets.only(left: 110.w),
              alignment: Alignment.topLeft,
              width: 375.w,
              margin: EdgeInsets.fromLTRB(
                10.w,
                0.w,
                10.w,
                5.w,
              ),
              child: Text(
                widget.item['discountDetails'] ?? '暂无使用说明',
                style: const TextStyle(
                  fontSize: 11,
                  fontWeight: FontWeight.w300,
                  color: Color.fromRGBO(
                    102,
                    102,
                    102,
                    1,
                  ),
                ),
              ),
            ))
      ],
    );
  }

  //获取优惠券针对用户
  String getType() {
    switch (widget.item['targetType']) {
      case 1:
        return '全场可用';
      case 2:
        return '权益卡会员可用';
      case 3:
        return '普通会员可用';
      case 4:
        return '新用户可用';
      case 5:
        return '部分用户参与';
      case 6:
        return '部分用户不参与';
      default:
        return '默认';
    }
  }
}
